/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import static com.railway.utils.RandomPassword.generateRandomPassword;
import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Moofie
 */
public class ForgetPassword extends javax.swing.JPanel {
    private JSONObject json;
    /**
     * Creates new form ForgetPassword
     */
    public ForgetPassword() {
        initComponents();
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jlblQuestion = new javax.swing.JLabel();
        jcbQuestion = new javax.swing.JComboBox();
        jtfAnswer = new javax.swing.JTextField();
        jlblAnswer = new javax.swing.JLabel();
        jtfID = new javax.swing.JTextField();
        jbtSubmit = new javax.swing.JButton();
        jbtBack = new javax.swing.JButton();

        setBackground(new java.awt.Color(46, 204, 113));
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Forget Password", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        jLabel1.setText("ID");

        jlblQuestion.setText("Question");

        jcbQuestion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "What was your childhood nickname?", "What street did you live on in third grade?", "In what city or town did your mother and father meet?", "What was the last name of your third grade teacher?", "What time of the day were you born?", "In what city or town was your first job?", "What was your favorite place to visit as a child?" }));

        jlblAnswer.setText("Answer");

        jbtSubmit.setText("Submit");
        jbtSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSubmitActionPerformed(evt);
            }
        });

        jbtBack.setText("Back");
        jbtBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jtfID))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jlblQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                                .addComponent(jlblAnswer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jcbQuestion, 0, 1, Short.MAX_VALUE)
                                .addComponent(jtfAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtBack, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblQuestion)
                    .addComponent(jcbQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAnswer)
                    .addComponent(jtfAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtSubmit)
                    .addComponent(jbtBack))
                .addContainerGap(136, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSubmitActionPerformed
        Map<String, Object> data = new HashMap<>();
        data.put("uid",jtfID.getText());
        data.put("question", jcbQuestion.getSelectedItem().toString());
        data.put("answer", jtfAnswer.getText());
        data.put("password",""+generateRandomPassword());
        
        String a = RestServer.httpPost("forgetPassword", data);
        System.out.println(a);
        try {
            json = new JSONObject(a);
            int status = json.getInt("status");
                if(status==200){
                    JSONObject content = json.getJSONObject("content");
                if(!content.getBoolean("err")){
                    if(content.getBoolean("exist")==true){
                        Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                    }else{
                        Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                    }
                }else{
                    Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                }
            }else{
                Toast.makeText(getParent(), "User not found!", Toast.LENGTH_LONG).display();
            }
        } catch (JSONException ex) {
            Toast.makeText(getParent(), "Connection error, Please try again later", Toast.LENGTH_LONG).display();
        }
    }//GEN-LAST:event_jbtSubmitActionPerformed

    private void jbtBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtBackActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jbtBackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbtBack;
    private javax.swing.JButton jbtSubmit;
    private javax.swing.JComboBox jcbQuestion;
    private javax.swing.JLabel jlblAnswer;
    private javax.swing.JLabel jlblQuestion;
    private javax.swing.JTextField jtfAnswer;
    private javax.swing.JTextField jtfID;
    // End of variables declaration//GEN-END:variables
}
